package com.yakami.ocean.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.squareup.picasso.Picasso;
import com.yakami.ocean.R;
import com.yakami.ocean.ServerAPI;
import com.yakami.ocean.adapter.base.BaseListAdapter;
import com.yakami.ocean.bean.News;
import com.yakami.ocean.utils.Tools;
import com.yakami.ocean.widget.squareimageview.SquareImageView;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author Yakami, Created on 2016/4/19
 */
public class NewsAdapter extends BaseListAdapter<News> {

    public NewsAdapter(Context context) {
        super(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NewsViewHolder(mInflater.inflate(R.layout.item_news, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NewsViewHolder newsHolder = (NewsViewHolder) holder;
        News tmp = getItem(position);
        newsHolder.title.setText(tmp.title);
        newsHolder.summary.setText(tmp.getSummary().trim());
        newsHolder.author.setText(tmp.author);
        newsHolder.date.setText(tmp.date);
        newsHolder.pageView.setText(String.valueOf(tmp.pageView));
        if (!Tools.isEmpty(tmp.imgUrl)) {
            try {
                Picasso.with(mContext).load(ServerAPI.getPreviewImgUrl(tmp.imgUrl)).into(newsHolder.previewImage);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //设置content_container的宽度
            newsHolder.itemView.post(() -> {
                        //判断是否已经处理过，防止多次处理
                        if (newsHolder.previewImage.getVisibility() == View.GONE) {
                            int height = newsHolder.container.getMeasuredHeight();
                            int width = newsHolder.container.getMeasuredWidth();
                            newsHolder.container.setLayoutParams(new LinearLayout.LayoutParams(width - height, height));
                            newsHolder.previewImage.setVisibility(View.VISIBLE);
                        }

                    }
            );
        }
    }


    class NewsViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.title) TextView title;
        @Bind(R.id.summary) TextView summary;
        @Bind(R.id.author) TextView author;
        @Bind(R.id.date) TextView date;
        @Bind(R.id.page_view) TextView pageView;
        @Bind(R.id.preview_image) SquareImageView previewImage;
        @Bind(R.id.content_container) LinearLayout container;

        public NewsViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            RxView.clicks(view)
                    .throttleFirst(500, TimeUnit.MILLISECONDS)
                    .subscribe((Void) -> {
                                if (mItemClickListener != null) {
                                    int pos = getAdapterPosition();
                                    mItemClickListener.onClick(pos, getItem(pos));
                                }
                            }
                    );
            RxView.longClicks(view)
                    .throttleFirst(500, TimeUnit.MILLISECONDS)
                    .subscribe((Void) -> {
                        if (mItemLongClickListener != null) {
                            int pos = getAdapterPosition();
                            mItemLongClickListener.onLongClick(pos, getItem(pos));
                        }
                    });
        }
    }
}
